﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RadarMonitor.CIMISS;
using RadarMonitor.DB;
using RadarMonitor;
using RadarMonitor.Alarm;

namespace RadarMonitor.ViewModel
{
    class CIMISSViewModel : INotifyPropertyChanged
    {

        private Setting.CIMISSConfig config;
        private RadarMonitorSource source = RadarMonitorSource.CIMISS;
        public CIMISSViewModel()
        {
            config = Setting.Setting.GetConfig().CIMISS;
            try
            {
                var list = DBHelper.Instance.lastRow(DBHelper.CIMISSTabel);
                LastCheckTime = (DateTime)list[1];
                //ResultCode = (int)list[2];
            }
            catch { }
            CIMISSMonitor.Instance.CIMISSAccessFailedEvent += Monitor_CIMISSAccessFailedEvent;
            CIMISSMonitor.Instance.RadarDataTransmissionSuccessed += Monitor_RadarDataTransmissionSuccessed;
            CIMISSMonitor.Instance.RadarDataTransmissionTimeoutEvent += Monitor_RadarDataTransmissionTimeoutEvent;
        }

        // 雷达文件未按时上传
        private void Monitor_RadarDataTransmissionTimeoutEvent(DateTime? arg1, string arg2)
        {
            DBHelper.Instance.insertBaseCimissRow( (int)(RadarMonitorResultCode.CIMISSFileUploadFailed));
            DBHelper.Instance.insertMonidorRow((int)source, (int)(RadarMonitorResultCode.CIMISSFileUploadFailed), null);
                
            ResultCode = (int)(RadarMonitorResultCode.CIMISSFileUploadFailed);
            AlarmManager.Alarm(source, RadarMonitorResultCode.CIMISSFileUploadFailed, null, config.DingDingEnabel, config.SmsEnabel, config.AudioEnabel);
            LastCheckTime = DateTime.Now;
        }

        private void Monitor_RadarDataTransmissionSuccessed(string file)
        {
            DBHelper.Instance.insertBaseCimissRow((int)(RadarMonitorResultCode.Success));
            LastCheckTime = DateTime.Now;
            ResultCode = (int)(RadarMonitorResultCode.Success);
        }

        private void Monitor_CIMISSAccessFailedEvent(string error)
        {

            LastCheckTime = DateTime.Now;
            ResultCode = (int)(RadarMonitorResultCode.CIMISSAcesssFailed);

            DBHelper.Instance.insertBaseCimissRow((int)(RadarMonitorResultCode.CIMISSAcesssFailed));
            DBHelper.Instance.insertMonidorRow((int)source, (int)(RadarMonitorResultCode.CIMISSAcesssFailed), error);
            AlarmManager.Alarm(source, RadarMonitorResultCode.CIMISSAcesssFailed, error, config.DingDingEnabel, config.SmsEnabel, config.AudioEnabel);
        }


        #region DataContext
        private bool _isWorking;

        public bool IsWorking
        {
            get { return _isWorking; }
            set
            {
                _isWorking = value;
                OnPropertyChanged("IsWorking");
            }
        }

        private DateTime _lastCheckTime;

        public DateTime LastCheckTime
        {
            get { return _lastCheckTime; }
            set
            {
                _lastCheckTime = value;
                OnPropertyChanged("LastCheckTime");
            }
        }

        private int _resultCode;

        public int ResultCode
        {
            get { return _resultCode; }
            set
            {
                _resultCode = value;
                OnPropertyChanged("ResultCode");
            }
        }


        #endregion

        #region Action
        public void start()
        {
            CIMISSMonitor.Instance.Start();
            IsWorking = true;
        }

        public void stop()
        {
            CIMISSMonitor.Instance.Stop();
            IsWorking = false;
        }

        public void toogle()
        {
            if (IsWorking)
            {
                stop();
            }
            else
            {
                start();
            }
        }
        #endregion


        #region 属性变化通知
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string propertyName = "")
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }



        #endregion
    }
}
